package think.thread.c02;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class MainThread {

	public static void main(String[] args) {
		ExecutorService service = Executors.newCachedThreadPool();
		for (int i = 0; i < 5; i++) {
			service.execute(new LiftOff());
		}
		service.shutdown();
		System.out.println("waiting for liftoff");
	}
}

class LiftOff implements Runnable {
	int countDown = 10;
	static int taskCount = 0;
	final int id = taskCount++;

	LiftOff() {
	}

	LiftOff(int countDown) {
		this.countDown = countDown;
	}

	String status() {
		return "#" + id + "(" + (countDown > 0 ? countDown : "liftoff!") + ").";
	}

	public void run() {
		try {
			while (countDown-- > 0) {
				System.out.print(status());
				TimeUnit.MILLISECONDS.sleep(10);
			}
			System.out.println();
			Thread.yield();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
